package com.hippo.core.vm

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.hippo.core.state.DeviceSettingState
import com.hippo.core.tuya.CommonResultCallback
import com.hippo.core.tuya.ThingControl
import com.hippo.core.tuya.ThingControl.devId
import com.thingclips.smart.home.sdk.ThingHomeSdk
import com.thingclips.smart.sdk.api.IThingDevice

class DeviceSettingVM:ViewModel() {


    var uiState by mutableStateOf(DeviceSettingState())

    private val thingDevice: IThingDevice = ThingHomeSdk.newDeviceInstance(devId)

    fun updateState(state: DeviceSettingState) {
        uiState = state
    }


    fun getDeviceBean(){
        val device = ThingControl.getDeviceBean()
        Log.i("sss", "getDeviceBean: $device")
        uiState = uiState.copy(device = device,showLoading = false)
    }

    fun renameDevice(name:String){

        uiState = uiState.copy(showLoading = true)

        thingDevice.renameDevice(name, object : CommonResultCallback() {
            override fun onSuccess() {
                getDeviceBean()
            }
        })
    }



    fun removeDevice(callback: ()->Unit){
        uiState = uiState.copy(showLoading = true)
        thingDevice.removeDevice(object : CommonResultCallback() {
            override fun onSuccess() {
                callback()
            }
        })
    }
}